/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package iconeLibrary.controler;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Iterator;

/**
 *
 * @author Ayede Sonia :: Gaudino Anthony :: Herche Bilal :: Teixeira Tiago :: Touati Samed  :: Wadjahdi Marc
 */
public class URLParser {

    private HashMap<String, ArrayList<String>> urlParsed;
    private String[] argNames = {"name", "size", "offsetX", "offsetY", "command"};

    /**
     * Method that return the arg name at the index .
     * @param index
     * @return
     */
    public String getArgName(int index) {
        if (index >= argNames.length || index < 0) {
            return null;
        }
        return argNames[index];
    }

    private void init(String[] argNames) {
        urlParsed = new HashMap<String, ArrayList<String>>();

        ArrayList<String> nomFiltre = new ArrayList<String>(1);

        ArrayList<String> nomIconeFinal = new ArrayList<String>(1);

        ArrayList<String> typeImage = new ArrayList<String>(1);

        for(String argName : argNames){
            urlParsed.put(argName, new ArrayList<String>());
        }
        urlParsed.put("nomFiltre", nomFiltre);
        urlParsed.put("nomIconeFinal", nomIconeFinal);
        urlParsed.put("typeImage", typeImage);
    }

    /**
     * Method that parse the url put in parameter .
     * @param URL needs to be parsed
     * @return HasMap< paramName , List >
     */
    public void parseURL(String URL) {

        init(argNames);

        String[] argValues = URL.split("/");

        urlParsed.get("typeImage").add(argValues[argValues.length - 1]);

        urlParsed.get("nomIconeFinal").add(argValues[argValues.length - 2]);

        parseURL(Arrays.copyOfRange(argValues, 0, argValues.length - 2), argNames);

    }

    /**
     * Method that control the url parsed .
     */
    public void controlParsedURL() {
        Iterator<String> it = urlParsed.keySet().iterator();

        while (it.hasNext()) {
            String key = it.next();
            if (key.equals("name") || key.equals("taille") || key.equals("offsetX") || key.equals("offsetY")) {
                if (urlParsed.get(key).isEmpty()) {
                    System.err.println("name, size , offsetX or offsetY are not present");
                }
                if (key.equals("taille")) {
                    for (String str : urlParsed.get(key)) {
                        try {
                            Float.valueOf(str);
                        } catch (NumberFormatException e) {
                            System.err.println("size must be a number");
                        }
                    }
                }
                if (key.startsWith("offset")) {
                    for (String str : urlParsed.get(key)) {
                        try {
                            Integer.valueOf(str);
                        } catch (NumberFormatException e) {
                            System.err.println("offset must be a number");
                        }
                    }
                }
            }
        }
    }

    /**
     * Method that parse the url.
     */
    private void parseURL(String[] argValues, String[] argNames) {

        for (int i = 0; i <
                argNames.length; i++) {
            if (i == argValues.length) {
                break;
            }

            String argValue = argValues[i];
            if (argNames[i].equals("command")) {
                if (argValue.equals("combine")) {
                    parseURL(Arrays.copyOfRange(argValues, i + 1, argValues.length), argNames);
                    continue;

                }

                if (argValue.equals("filtre")) {
                    urlParsed.get(argNames[i]).add(argValue);
                    urlParsed.get("nomFiltre").add(argValues[i + 1]);
                    continue;
                }
            }
            urlParsed.get(argNames[i]).add(argValue);
        }

    }

    /** 
     * Method that print the url parsed .
     */
    public void printParsedURL() {
        Iterator<String> it = urlParsed.keySet().iterator();

        while (it.hasNext()) {
            String key = it.next();

            System.out.print(key + " = ");
            for (String str : urlParsed.get(key)) {
                System.out.print(str + " ");
            }

            System.out.print("\n");
        }
    }

    /** 
     * Method that returns the url parsed .
     * @return url parsed .
     */
    public HashMap<String, ArrayList<String>> getUrlParsed() {
        return urlParsed;
    }
}
